const express = require('express');
const multer = require('multer');
const router = express.Router();
const UploadController = require('../controllers/uploadController');
const { upload } = require('../config/upload');
const { authenticateToken } = require('../middleware/authMiddleware');

// 单文件上传
router.post('/single', authenticateToken, upload.single('file'), UploadController.singleUpload);

// 多文件上传
router.post('/multiple', authenticateToken, upload.array('files', 5), UploadController.multipleUpload);

// 错误处理中间件
router.use((error, req, res, next) => {
  if (error instanceof multer.MulterError) {
    if (error.code === 'LIMIT_FILE_SIZE') {
      return res.status(400).json({ message: '文件大小超出限制' });
    }
    if (error.code === 'LIMIT_FILE_COUNT') {
      return res.status(400).json({ message: '文件数量超出限制' });
    }
  }
  res.status(400).json({ message: error.message });
});

module.exports = router;